package logic.action;

import java.util.List;

import logic.action.base.AbstractAsyncUIData;
import logic.action.base.AsyncUIAction;
import logic.bean.MyNoteInfo;
import logic.constants.Const;
import logic.constants.DefaultConsts;
import logic.dao.BookMarksDao;
import android.content.Context;
import android.os.Message;
import base.tina.core.task.Task;
import base.tina.core.task.TaskService;
import base.tina.core.task.infc.ITaskResult;

import com.shwread.android.qysw.activity.BaseBusinessActivity;


public class GetAllBookNoteAction<T extends BaseBusinessActivity>
        extends
        AsyncUIAction<T>
{
	
	AllBookNotesListener allBookNotesListener;
	
	public GetAllBookNoteAction(T baseActivity) {
		super(baseActivity);
	}
	
	@Override
	public boolean handleResult(ITaskResult taskOrResult, TaskService service) {
		boolean ishandled = true;
		switch (taskOrResult.getSerialNum()) {
			case GetAllBookNoteResult.SerialNum:
				GetAllBookNoteResult result = (GetAllBookNoteResult) taskOrResult;
				Message msg = Message.obtain();
				msg.what = DefaultConsts.UPDATEUI_ALL_BOOK_NOTE;
				allBookNotesListener.setAllBookInfos(result.infos);
				
				bActivity.getUIHandler().sendMessage(msg);
				break;
			default:
				ishandled = false;
		}
		return ishandled;
	}
	
	@Override
	public boolean exCaught(ITaskResult task, TaskService service) {
		boolean ishandled = true;
		switch (task.getSerialNum()) {
			case GetAllBookNoteTask.SerialNum:
				break;
			default:
				ishandled = false;
		}
		return ishandled;
	}
	
	@Override
	public void create() {
		
	}
	
	@Override
	public <E> void start(E e) {
		bActivity.mAService.requestService(new GetAllBookNoteTask(bActivity), getBindSerial());
	}
	
	public void OnSearchInfoListener(AllBookNotesListener allBookNotesListener) {
		this.allBookNotesListener = allBookNotesListener;
	}
	
	public interface AllBookNotesListener
	{
		public void setAllBookInfos(List<MyNoteInfo> infos);
	}
	
	public class GetAllBookNoteTask
	        extends
	        Task
	{
		Context context;
		
		public GetAllBookNoteTask(Context context) {
			super(0);
			this.context = context;
		}
		
		@Override
		public void run() throws Exception {
			GetAllBookNoteResult result = new GetAllBookNoteResult();
			
			result.infos = BookMarksDao.getInstance(context).loadMyBookNotes(Const.phone_number);
			
			System.out.println("我的笔记 task 笔记数");
			
			commitResult(result, CommitAction.WAKE_UP);
		}
		
		@Override
		public int getSerialNum() {
			return SerialNum;
		}
		
		public static final int SerialNum = -GetAllBookNoteResult.SerialNum;
		
	}
	
	static class GetAllBookNoteResult
	        extends
	        AbstractAsyncUIData
	{
		public List<MyNoteInfo> infos;
		
		@Override
		public int getSerialNum() {
			return SerialNum;
		}
		
		public static final int SerialNum = GET_MY_ALL_BOOKNOTE;
		
	}
	
}
